package com.hanrui.android.activity.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hanrui.android.R;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.model.TaskBackForm;
import com.hanrui.android.net.contract.message.BackTaskContract;
import com.hanrui.android.net.presenter.message.BackTaskPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoBackActivity extends ActionBarBaseActivity<BackTaskPresenter> implements BackTaskContract.View {

    String taskId,procInsId,bizId;

    @BindView(R.id.task_ll)
    LinearLayout taskLl;
    @BindView(R.id.task)
    TextView task;
    @BindView(R.id.lastusername)
    TextView lastusername;
    @BindView(R.id.lasttime)
    TextView lasttime;
    @BindView(R.id.reson)
    EditText reson;
    @BindView(R.id.login_out)
    TextView login_out;

    String assigneeId;
    String activityId;

    OptionsPickerView userOptions;

    @Override
    protected void initInjector() {mActivityComponent.inject(this);}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goback);

        unbinder = ButterKnife.bind(this);

        taskId = getIntent().getStringExtra("taskId");
        procInsId = getIntent().getStringExtra("procInsId");
        bizId = getIntent().getStringExtra("bizId");

        mPresenter.taskBackForm(this,procInsId);


    }

    public static void start(Context context, String taskId, String procInsId, String bizId){

        Intent intent = new Intent(context,GoBackActivity.class);
        intent.putExtra("taskId",taskId);
        intent.putExtra("procInsId",procInsId);
        intent.putExtra("bizId",bizId);
        context.startActivity(intent);

    }

    @Override
    public void taskBackForm(final List<TaskBackForm.ListDataBean> list) {
        if (null != list && list.size() > 0) {
            task.setText(list.get(0).getActivityName());
            assigneeId = list.get(0).getAssigneeId();
            activityId = list.get(0).getActivityId();
            lastusername.setText(list.get(0).getAssigneeName());
            lasttime.setText(list.get(0).getEndTime());
            userOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    task.setText(list.get(options1).getPickerViewText());
                    assigneeId = list.get(options1).getAssigneeId();
                    activityId = list.get(options1).getActivityId();
                    lastusername.setText(list.get(options1).getAssigneeName());
                    lasttime.setText(list.get(options1).getEndTime());
                }
            }).build();
            userOptions.setPicker(list);
        }
    }

    @Override
    public void taskBack(int code) {
        if (code == 1) {
            ToastUtils.showShort("退回成功");
            this.finish();
        } else {
            ToastUtils.showShort("退回失败");
        }
    }


    @OnClick({R.id.task_ll,R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.task_ll:
                if (userOptions != null) {
                    userOptions.show();
                }
                break;
            case R.id.login_out:
                String content = reson.getText().toString().trim();
                if (StringUtils.isEmpty(content)) {
                    ToastUtils.showShort("退回意见不能为空！");
                    return;
                }
                mPresenter.taskBack(this,taskId,content,assigneeId,activityId,procInsId);
                break;

        }
    }
}
